<?php

namespace App\Http\Controllers\Resources;

use App\Http\Requests\AdminPost;
use function foo\func;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AdminController extends Controller
{

    /**
     * 初始化
     *
     * CategoryController constructor.
     * @param $_resource
     */
    public function __construct($_resource)
    {
        $this->_resource = $_resource;
    }

    /**
     * @param AdminPost $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(AdminPost $request)
    {
        $inputs = ($request->all());
        $path = '';
        $_filter = [];
        //查询条件
        if (!empty($inputs)) {
            if (!is_null($inputs['options']) && !is_null($inputs['keywords'])) {
                $_filter['where'][] = function ($query) use ($inputs) {
                    $query->where($inputs['options'], "LIKE", '%' . $inputs['keywords'] . '%');
                };
                $path .= '?&options=' . $inputs['options'] . '&keywords=' . $inputs['keywords'];
            }
        }

        //排序
        $_order = ['id' => 'DESC'];
//        dd($_filter);
        $result = $this->_CallbackService()->index($_filter, $_order, 20);
        $sun = count($result);
        return view('resources.admin.index', compact('result','inputs','sun'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('resources.admin.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(AdminPost $request)
    {
        $inputs = $request->all();
        $result = $this->_CallbackService()->store($inputs);
        if ($result) {
            return redirect('admin');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $field = $this->_CallbackService()->show(['id',$id]);
        return view('resources.admin.edit',compact('field'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(AdminPost $request, $id)
    {
        $inputs = $request->all();
        if ($inputs['password'] == ''){
            unset($inputs['password']);
            unset($inputs['password_confirmation']);
        }else{
            $this->validate(\request(),['password' => 'string|min:6|confirmed']);
            $inputs['password'] = bcrypt($inputs['password']);
            unset($inputs['password_confirmation']);
        }
        $request = $this->_CallbackService()->update(['id'=>$id],$inputs);
        if ($request){
            return redirect('/admin');
        }else{
            $this->edit($id);
        }
    }

    /**
     * @param $id
     * @return array
     */
    public function destroy($id)
    {
        $field = $this->_CallbackService()->show(['id',$id]);
        if ($field){
            $result = $this->_CallbackService()->destroy(['id'=> $field->id]);
            if ($result){
                return $this->_msg('删除成功');
            }
        }else{
            return $this->_error('操作错误');
        }
    }
}
